#coding=utf-8

import settings
import random
from core.scanner import FilterManager
from core.meta import MetaHandler
from core.ViewMaker import ViewMaker
from core.tools import htqsValidate

"""
增加 htqs 加密拦截
"""
if getattr(settings,'ENABLE_HTQS', None) is True:
    if not hasattr(settings,'HTQS_HASH_SALT') or not hasattr(settings,'HTQS_TIME_OUT'):
        raise Exception('Must Set HTQS_HASH_SALT and HTQS_TIME_OUT para in your setting.py file')


    @FilterManager.register('htqs')
    def htqsFilter(httpReq):
        if not isinstance(httpReq, MetaHandler):
            return ViewMaker.httpStatusView(500, "httpReq is not Servlet Request !")

        qsStr = httpReq.get_query_string()
        checkFlag = htqsValidate(settings.HTQS_HASH_SALT, settings.HTQS_TIME_OUT, qsStr)


        if checkFlag is True :
            return ViewMaker.chainView()
        else:
            return ViewMaker.httpStatusView(500, "Forbidden, HTQS check False ! ")

def htmlFilter(httpReq):
    if not isinstance(httpReq, MetaHandler):
        return ViewMaker.httpStatusView(500, "httpReq is not Servlet Request !")
    path = httpReq.get_uri()
    print path
    if path.endswith('.html'):
        return ViewMaker.htmlView(path,{})
    else:
        return ViewMaker.chainView()


#TODO 登录filter
#TODO realTransactionMap 用户、账户、策略绑定 Filter